//
//  newsViewController.m
//  唧歪
//
//  Created by MWJ on 15/9/4.
//  Copyright (c) 2015年 MWJ. All rights reserved.
//

#import "newsViewController.h"
#import "AppDelegate.h"
#import "newsCell.h"
#import "newsmodel.h"
#import "PostDetailsViewController.h"

#define newsremoveURL @"https://app.jiwaipro.com:8888/index.php/JiWai/newsremove"

@interface newsViewController (){
    AppDelegate *app;
    NSMutableArray *newsArray;
    NSMutableArray *messageArray;
    NSString *usernews;
}

@end

@implementation newsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    newsArray = [NSMutableArray array];
    messageArray = [NSMutableArray array];
    app = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    for (NSDictionary *dic in app.appnewsArray) {
        newsmodel *newsm = [newsmodel alloc];
        newsm.content = dic[@"news_content"];
        newsm.news_id = dic[@"news_id"];
        newsm.news_messageId = dic[@"news_messageId"];
        newsm.news_userId = dic[@"news_userId"];
        [newsArray addObject:newsm];
    }
    [self.tableView reloadData];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 返回tableview的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return newsArray.count;
}

#pragma mark - 返回tableview的高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}
#pragma mark - cell的内容加载
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UINib *nib=[UINib nibWithNibName:@"newsCell" bundle:nil];
    [self.tableView registerNib:nib forCellReuseIdentifier:@"newsCell"];
    
    static NSString *cellIdentifier=@"newsCell";
    newsCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    newsmodel *newsmm = newsArray[indexPath.row];
    [cell.title setFont:[UIFont boldSystemFontOfSize:16.0f]];
    cell.title.text = [NSString stringWithFormat:@"%@",newsmm.content];
    cell.message_id = [NSString stringWithFormat:@"%@",newsmm.news_messageId];
    [messageArray addObject:cell.message_id];
    return cell;
}
#pragma mark - cell的点击事件
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *poststr =[NSString stringWithFormat:@"message_id=%@",messageArray[indexPath.row]];
    [self newsPath:newsremoveURL Str:poststr];
    app.appmessageid = messageArray[indexPath.row];
    PostDetailsViewController *postDetail=[self.storyboard instantiateViewControllerWithIdentifier:@"detailVC"];
    [self.navigationController pushViewController:postDetail animated:YES];
}
-(void)newsPath:(NSString *)path Str:(NSString *)string{
    NSURL *url = [NSURL URLWithString:path];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"POST"];
    NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
    [request setHTTPBody:data];
    [NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        usernews = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
        [self performSelectorOnMainThread:@selector(refreshDatanews:) withObject:usernews waitUntilDone:YES];
    }];
}
-(void)refreshDatanews:(NSString *)sender{
}

@end
